var arrBT = new Array(51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 84, 85, 86);
var arrBTMinNum = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0);
var arrBTMaxNum = new Array(9, 1, 1, 9, 1, 1, 9, 1, 1, 9, 21, 1, 1, 1, 1, 1);
var arrOddsPlus = new Array(0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1);

function NumFormat(intNum){
	return (intNum < 10)?("0" + intNum.toString()):intNum.toString();
}

function clearTextField(){
	var i = 0, intBT = 0, intNum = 0;
	for(i = 0; i < arrBT.length; i++){
		intBT = arrBT[i];

		for (intNum = arrBTMinNum[i]; intNum <= arrBTMaxNum[i]; intNum++){
			if(null != document.ShowOddsForm["Odds"+intBT.toString()+"_"+intNum.toString()]){
				document.ShowOddsForm["Odds"+intBT.toString()+"_"+intNum.toString()].value = "";
			}
		}
	}
}

function GetWagerTable(){
	if(document.getElementById("OddsID51_0") && document.getElementById("OddsID61_21")){
		InsertOdds();
		return;
	}
}

function InsertOdds(){
	var ArrGame;
	var newTD1, newTD2, newTD3, intNum = 0;
	var i = 0, intBT = 0;

	for(i = 0; i < arrBT.length; i++){
		intBT = arrBT[i];
		ArrGame = eval("parent.Game"+intBT.toString());

		for (intNum = arrBTMinNum[i]; intNum <= arrBTMaxNum[i]; intNum++){
			newTD1 = document.getElementById("OddsID"+intBT.toString()+"_"+intNum.toString());
			//newTD2 = document.getElementById("OddsTip"+intBT.toString()+"_"+intNum.toString());
			newTD3 = document.getElementById("OddsValue"+intBT.toString()+"_"+intNum.toString());
			if(ArrGame[intNum][6] == "1"){
				newTD1.className = "mbline_ratio_stop";
				newTD3.className = "mbline_stop";
			}else{
				newTD1.className = "mbline_ratio";
				newTD3.className = "mbline";
			}
			newTD1.innerHTML = (ArrGame[intNum][6] == "1")?"":Math.round((arrOddsPlus[i] + parseFloat(ArrGame[intNum][5]))*1000)/1000;
		}
	}
}

function CheckNumKey(){
	if(event.keyCode == 13) return true;
	if((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode > 95 || event.keyCode < 106)){
		alert(notNumber);
		return false;
	}
}

function OtherCheck(objGold, flOdds, intBID, intNum){
	var intChildEveLimitUsed = 0;
	if(intBID == 1)intNum -= 1;
	//下注金額必須為數值
	if(isNaN(objGold.value) == true){
		objGold.focus();
		alert(notNumber);
		return false;
	}
	//不能低於最低下注金額
	if(eval(objGold.value) < chkMinStake[intBID]){
		objGold.focus();
		alert(underMinStake+chkMinStake[intBID].toString());
		return false;
	}
	//不能超過本期單注限額
	if(eval(objGold.value) > chkLottoMaxBetLimitValue[intBID]){
		objGold.focus();
		alert(overLottoMaxBetLimitValue+chkLottoMaxBetLimitValue[intBID]);
		return false;
	}
	//不能超過單注限額
	if(eval(objGold.value) > chkMaxBetLimitValue[intBID]){
		objGold.focus();
		alert(overBetLimit + chkMaxBetLimitValue[intBID]);
		return false;
	}
	//不能超過單場限額
	intChildEveLimitUsed = arrChildEveLimitUsed[intBID][(intBID == 61?intNum:intNum-6)];
	if(eval(objGold.value) > (chkMaxEventLimitValue[intBID] - intChildEveLimitUsed)){
		objGold.focus();
		alert(overEventLimit + (chkMaxEventLimitValue[intBID] - intChildEveLimitUsed).toString());
		return false;
	}
	//不能超過最高派彩限額
	if((eval(objGold.value)*eval(flOdds)) > chkMaxPayOff[intBID]){
		objGold.focus();
		alert(overMaxPayOff+chkMaxPayOff[intBID]);
		return false;
	} 
}

function FinalCheck(){
	var objGold, intNum = 0, intTotal = 0, strWager = "";
	var ArrGame;
	var i = 0, intBT = 0;



	for(i = 0; i < arrBT.length; i++){
		intBT = arrBT[i];
		ArrGame = eval("parent.Game"+intBT.toString());

		for (intNum = arrBTMinNum[i]; intNum <= arrBTMaxNum[i]; intNum++){
			objGold = document.ShowOddsForm["Odds" + intBT.toString() + "_" + intNum.toString()];

			if (OtherCheck(objGold, ArrGame[intNum][5], intBT, intNum) == false) return;
			if(objGold.value == "")continue;

			strWager += intNum.toString() + "#" + ArrGame[intNum][4] + "#" + objGold.value + "#";
			strWager += intBT.toString() +"@";
			intTotal += parseInt(objGold.value);
		}
	}
	//請填入下注金額！
	if (intTotal == 0)	{
		alert(noMoney);
		return false;
	}
	//總金額不能超過信用餘額
	if (intTotal > chkCredit) {
		alert(overCredit+chkCredit);
		return false;
	}
	//確定下注？
	if (!confirm(sureStake))	return false;
	document.CommonWagerForm.WagerContent.value = strWager;
	document.CommonWagerForm.submit();
	document.GetOddsForm.submit();
}

function Calculate(){
	var intTotal = 0, objGold;
	var i = 0, intBT = 0, intNum = 0;

	for(i = 0; i < arrBT.length; i++){
		intBT = arrBT[i];
		for (intNum = arrBTMinNum[i]; intNum <= arrBTMaxNum[i]; intNum++){
			objGold = document.ShowOddsForm["Odds" + intBT.toString() + "_" + intNum.toString()];
			//alert("Odds" + intBT.toString() + "_" + intNum.toString());
			if(objGold.value == "")continue;
			//alert(2);
			intTotal += parseInt(objGold.value);
		}
	}
	//document.getElementById("StakeMoney").innerHTML = intTotal;
}

function CountRefresh(){
	var intGT = 0, intGT2 = 0, strGTm = "", strGTs = "";
	if(intRefreshSec >= RefreshSec)isRefresh = true;
	intGT = parseInt(parent.strEndGameTime);
	if(intGT < 0)intGT = 0;
	intGT2 = (intGT > 900)?900:intGT;
	strGTm = (Math.floor(intGT2 / 60)).toString();
	if(strGTm.length < 2)strGTm = "0" + strGTm;
	strGTs = (intGT2 % 60).toString();
	if(strGTs.length < 2)strGTs = "0" + strGTs;
	document.getElementById("GameSec").innerHTML = strGTm+":"+strGTs;
	parent.strEndGameTime = intGT - 1;
	intRefreshSec++;
}

function SubmitOddsForm(){
	if(isRefresh == false){
		document.GetOddsForm.action = "http://tw.i5.yimg.com/i/tw/hp/spirit/w5.gif";
		document.GetOddsForm.submit();
		window.status = "";
		return;
	}
	document.GetOddsForm.action = "number51_odds.php";
	document.GetOddsForm.submit();
	isRefresh = false;
	intRefreshSec = 0;
}

function InitAction(){
	document.GetOddsForm.submit();
	setIntervalID1 = setInterval("SubmitOddsForm();", 30000);
	setIntervalID2 = setInterval("CountRefresh();", 1000);
}

function DisplayWindow(){
	var i = 0;
	score_window.style.display = parent.strDisplay;
	if(parent.strDisplay != "none"){
		for(var i = 0; i < parent.arrImages.length; i++){
			document.getElementById("image" + (i + 1)).src = "../images/ball/"+parent.arrImages[i]+".gif";
		}
	}
}

function getStakeGold(){
	if(document.getElementById("StakeGold").value == "")return "";
	return eval(document.getElementById("StakeGold").value);
}

function getNumArray(){
	return new Array(1, 49);
}
